<?php
//1、设置编码utf8
header("Content-Type: text/html; charset=UTF-8");



require("conn.php");

 

$image1= $_FILES['pic1'];
$image2= $_FILES['pic2'];
$image3= $_FILES['pic3'];

$img_arr =array($image1,$image2,$image3);
$up = upimgs($img_arr);

 function upimgs($img_arr){
$nub = count($img_arr);
$img_arr2 =Array();
$k=0;
 //print_r($img_arr[0]);
for ($n=0; $n <count($img_arr) ; $n++) { 

 if ($img_arr[$n]['name']!='') {
  $img_arr2[$k]= $img_arr[$n];
  $k++;
 }
}
$tp = array("image/pjpeg","image/jpeg","image/png"); 
$wancheng = Array();
$k1= 0;

for ($i=0; $i <count($img_arr2) ; $i++) { 
 $shangchuan = $img_arr[$i];
$shifou='';
for ($i2=0; $i2 <count($tp); $i2++) { 
 if ($tp[$i2]==$shangchuan['type']) {
   $shifou=1;
 }
}
if ($shifou!=1) {
 echo "<script language='javascript'>alert('添加失败，".$shangchuan['name']."格式错误！ ');history.go(-1)</script>";
 return false;
}
//随机文件名
$name1 =time();
$base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$count = strlen($base);
$random = '';
for ($i3=0; $i3 < 16; $i3++) { 
 $random.=$base[rand(0,$count-1)];
}

//图片类型获取
$houzui = get_file_type($shangchuan['name']);
$name2 = $name1.$random.".".$houzui;
$name2 = "upload/".$name2;

//7、移动临时文件到上传的文件存放位置（核心代码）
copy($shangchuan['tmp_name'], $name2);


 if (file_exists($name2)=='ture'){
  
 $wancheng[$k1] = $name2;
  $k1++;

 }else{
  echo "<script language='javascript'>alert('添加失败，".$shangchuan['name']."格式错误！ ');history.go(-1)</script>";
 }



}

if (count($wancheng)== count($img_arr2)) {
  $allimg = implode(",",$wancheng);
setdata($allimg);
}



}


//**********************************************
function base64EncodeImage ($image_file) {

$base64_image = '';

$image_info = getimagesize($image_file);

$image_data = fread(fopen($image_file, 'r'), filesize($image_file));

$base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));

return $base64_image;

}
//**********************************************

function get_file_type($filename)
{  
   $type =explode("." , $filename);
   $count=count($type)-1;
   return $type[$count];
}

function setdata($allimg){

$title=@$_POST['title'];
  
    $bianhao=@$_POST['bianhao'];
    $pic=@$_POST['pic'];
    $content=@$_POST['content'];

  
     if($title==""){
      echo "<script language='javascript'>alert('请输入公告主题！');history.go(-1)</script>";
     return false;
   }
     
     if($pic1==""){
        echo "<script language='javascript'>alert('请上传图片！');history.go(-1)</script>";
     return false;
     }
      
  
$sql1="INSERT INTO notice(title,jia,chuban,shu,shu2,youfei,zuoze,bianhao,fahuodizhi,xiangqing,pic1) VALUES ('".$title."','".$jia."','".$chuban."',".$shu.",".$shu.",'".$youfei."','".$zuoze."','".$bianhao."','".$fahuo."','".$xiangqing."','".$pic1."')";
  if (mysql_query($sql1)){   

    echo "<script language='javascript'>alert('新添加商品成功！');window.location.href='renwu1.php?lei=qq&a=reg';</script>";
     }else{
        echo  mysql_error();
        die();
   echo "<script language='javascript'>alert('添加失败！');history.go(-1)</script>";
     }
    
}